/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.forms;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author lycdragon
 */
public class AddHolidayForm extends org.apache.struts.action.ActionForm {

    private DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    private Date startDate;
    private Date endDate;
    private String startDateString;
    private String endDateString;

    public String getEndDateString() {
        return endDateString;
    }

    public void setEndDateString(String endDateString) {
        this.endDateString = endDateString;
        try {
            this.endDate = df.parse(endDateString);
        } catch (ParseException ex) {
            Logger.getLogger(SalesReportActionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getStartDateString() {
        return startDateString;
    }

    public void setStartDateString(String startDateString) {
        this.startDateString = startDateString;
        try {
            this.startDate = df.parse(startDateString);
        } catch (ParseException ex) {
            Logger.getLogger(SalesReportActionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    /**
     *
     */
    public AddHolidayForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getStartDateString() == null || getStartDateString().length() < 1
                || getEndDateString() == null || getEndDateString().length() < 1) {
            errors.add("holiday", new ActionMessage("error.holiday.nullDate"));
        } else if (getEndDate().before(getStartDate())) {
            errors.add("holiday", new ActionMessage("error.holiday.invalid"));
        }

        if (getDescription() == null || getDescription().length() < 1){
            errors.add("holiday", new ActionMessage("error.holiday.nulldescription"));
        }

        return errors;
    }
}
